package com.xian.tszs.meal.service.impl;

import java.util.Date;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xian.tszs.meal.entity.TsUser;
import com.xian.tszs.meal.mapper.TsUserMapper;
import com.xian.tszs.meal.repository.TsUserRepository;
import com.xian.tszs.meal.server.TokenService;
import com.xian.tszs.meal.service.TsUserManageService;
import com.xian.tszs.meal.utils.UUIDUtils;

@Service
public class TsUserManageServiceImpl implements TsUserManageService {
	
	
	@Autowired
	private TsUserMapper tsUserMapper;
	
	@Autowired
	private TokenService tokenService;
	
	@Autowired
	private TsUserRepository tsUserRep;
	
	@Override
	public TsUser findByOpenId(String openId) {
		return tsUserMapper.findByOpenId(openId);
	}

	@Override
	public String sendToken(String openId) {
		
		TsUser user = tsUserMapper.findByOpenId(openId);
		
		if (user == null) {
			//新增一个用户
			user = new TsUser();
			user.setId(UUIDUtils.getUUID());
			user.setOpenId(openId);
			user.setCreateTime(new Date());
			tsUserRep.save(user);
		}
		String token = tokenService.getToken(user);
		
		
		return token;
	}

	@Override
	public void updateUserInfo(TsUser tsUser) {
		tsUserMapper.updateUserInfo(tsUser);
	}

	@Override
	public void updateUserPhone(String phone,String id) {
		// TODO Auto-generated method stub
		tsUserMapper.updateUserPhone(phone,id);
	}

}
